package com.monboo.media
{
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;

	public class Music
	{
		private var _musicUrl:String;
		private var channel:SoundChannel;
		private var music:Sound;
		
		public function Music(musicUrl:String){
			
			_musicUrl = musicUrl;
			
			loadMusic();
		}
		
		private function loadMusic():void{
			music = new Sound(new URLRequest(_musicUrl));
			music.addEventListener(IOErrorEvent.IO_ERROR, error);
			
			try {
				channel = music.play(0,int.MAX_VALUE);
			}catch (err:Error){
				trace("流错误");
			}
		}
		private function error(e:IOErrorEvent):void{
			 // 处理io错误。
		}
		public function setVolume(volume:Number):void {
			if(channel){
				var transform:SoundTransform = channel.soundTransform;
				transform.volume = volume;
				channel.soundTransform = transform;
			}
		}
		
	}
}
